<?php

require_once '../AopCertClient.php';
require_once '../AopCertification.php';
require_once '../request/AlipayTradeQueryRequest.php';
require_once '../request/AlipayTradeWapPayRequest.php';
require_once '../request/AlipayTradeAppPayRequest.php';


/**
 * 证书类型AopCertClient功能方法使用测试，特别注意支付宝根证书预计2037年会过期，请在适当时间下载更新支付更证书
 * 1、execute 证书模式调用示例
 * 2、sdkExecute 证书模式调用示例
 * 3、pageExecute 证书模式调用示例
 */

//2、sdkExecute 测试
$aop = new AopCertClient ();

$appCertPath = "./appCertPublicKey_2021001142684784.crt";
$alipayCertPath = "./alipayCertPublicKey_RSA2.crt";
$rootCertPath = "./alipayRootCert.crt";

$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId ='2021001142684784';
$aop->rsaPrivateKey = 'MIIEogIBAAKCAQEAgbCtPmXjeb8O/pYvqWZ5vJZRDf76qXNHb9XYyqUATh3NW/EJpCL+N5PfX0iyqHcVQWu/9C3J48J6rCshxISFSnH65uYgQiZR2yuS6TS3vZd5mQrOLhQRsnP9ZD658bMeWwu+XLcx58gPYpE0dKwtRTp9CKn8fFoXLN2kzySNM6wGWSpZwQUkPZcl09mn5nz0/JutywX9tOurkNdt/51x675AN8ciaGpie8lWP9SXMbSGFdtbEEOxeAtuNpLY8k+EwCz1bGaeIX5vofuwbqL/acrNhyNLI+iM0AS5g0JiQ7UQB7OXvih8WNLPsoOWfyidJKJAJG8EMdZPi3/mZym/6wIDAQABAoIBAFrVsiG4H2FQTmJGMs/FdJJ//fl9k3dHxMsxzovwkYwlF4bEBmquD7xBl/wW4PtZnMubenC9+V/LATzBYP9RikIgHwnGOOXcDQ0jMdyhDeh+xa5JgSuI5vREhT/HJ1mAiUxfp08tCIk8+HmjFkh5p6OLWbPNy4gVWZIVE6/5++QqIqGUAtuuanFqZVHm/z2FySzdWhydAl6YBCwpn81zEsQeG+bkOANWlrN5F3kAFDChzuZPZAv386xi7NpQ0ZruPnyy9bElfo+LGX09lADYmZtmP1C4/vSwhblznXrEiVYYPtu6czTS4Bcqw9T9RUa+6Lf8eCZq+zAET5IrmTs1yvECgYEAxVjNPlAqWIKgJT0pih3p4DBO7anjg4zm/4ZQal2mUaiE6tnLcT6X877vTsXKclIlZJ7wKNorzJVHfL0gzX1xnqrVQJ5n9svHP1Ym1nfmG4ET52nss6nqIM/zvX2kZMzPHXPGvy9MBwDZWY3KLwjO05hG5dhBG/MSsdkvuijsCTcCgYEAqDww9NlZ62Xl9F+EfGP4FvsLSs9NHTugC7WttVL/+FiimHQ8MRUJ0vgPL1Gp90VdDQ07KJmWuv/5k67Xq8ysaSxj6CK8206/AIJ+AlE9WcOpOEgcTZTsSGhIoKfgjsn6HsVacR45whWUdGTD9zevdzbR4GCS9I3Ig9aiyL5AiO0CgYAnV3mrJ34ZoQjeA6BpWWdLezXw6UJi8k8Gt7cGTV8fvlGpI/wLume+ftzkPkgKnFe6BHlqG3xJz6StuGJ2tMBW2kWjGTEuoRc6iCk77jEgYI4gPxyiAIidO/eiWg7QzYeonnELohks+D1cPgkFcWMSCbRwk7j/ImkirWJ3YTsU5wKBgFCMURxKHIVaSllQikxMSPInzGZLDq7aZ2xz1fGlBa1QMrkvncoaOpPp9MtBHFGpwj2HgjWUI9jwyfvtkJFO85V/jW4Ng8K01zpVQCO35dnelly+Gd8DWIXqeCuDdT6ocbrd3smeISVN08BOpozs+nPnqWD2nCW+5KxKzbGzdY6ZAoGAbH5QzQNJiwzNczALZPE2REHi+RAFE+d5RDqRlYwBr4F+K9ChSwUURsirp1qze/WXMThzziIuY2m/57+rC8QvGFT24n2oAhcP0Rz+mVmsUQbzNVr2eY1kSkjXIsMTwTQye8ky632FV7urM+IOqcrWfIHgAPtNFAROZXrRtoktqsQ=';
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'utf-8';
$aop->format = 'json';
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
// $aop->alipayRootCertSN = '687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6';//调用getRootCertSN获取支付宝根证书序列号

$request = new AlipayTradeAppPayRequest ();
$date=date("YmdHis");
$arr=range(1000,9999);
shuffle($arr);
$request->setBizContent("{\"out_trade_no\":\"".$date.$arr[0]."\",\"total_amount\":0.01,\"product_code\":\"QUICK_MSECURITY_PAY\",\"subject\":\"app测试\"}");

$result = $aop->sdkExecute($request);
echo(htmlspecialchars($result));die;
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
    echo "成功";
} else {
    echo "失败";
}

die;
//3、pageExecute 测试
$aop = new AopCertClient ();
$appCertPath = "应用证书路径（要确保证书文件可读），例如：/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = "支付宝公钥证书路径（要确保证书文件可读），例如：/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = "支付宝根证书路径（要确保证书文件可读），例如：/home/admin/cert/alipayRootCert.crt";

$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = '你的appid';
$aop->rsaPrivateKey = '你的应用私钥';
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'utf-8';
$aop->format = 'json';
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号

$request = new AlipayTradeWapPayRequest ();
$request->setBizContent("{" .
    "    \"body\":\"对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。\"," .
    "    \"subject\":\"测试\"," .
    "    \"out_trade_no\":\"70501111111S001111119\"," .
    "    \"timeout_express\":\"90m\"," .
    "    \"total_amount\":9.00," .
    "    \"product_code\":\"QUICK_WAP_WAY\"" .
    "  }");
$result = $aop->pageExecute($request);
echo $result;


